/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package smpptester;


import java.io.UnsupportedEncodingException;
import org.jsmpp.bean.AlertNotification;
import org.jsmpp.bean.DataSm;
import org.jsmpp.bean.DeliverSm;
import org.jsmpp.bean.DeliveryReceipt;
import org.jsmpp.bean.MessageType;
import org.jsmpp.extra.ProcessRequestException;
import org.jsmpp.session.DataSmResult;
import org.jsmpp.session.MessageReceiverListener;
import org.jsmpp.session.Session;
import org.jsmpp.util.InvalidDeliveryReceiptException;

/**
* @author uudashr
*
*/
public class MessageReceiverListenerImpl implements MessageReceiverListener {

    @Override
public void onAcceptDeliverSm(DeliverSm deliverSm)
throws ProcessRequestException {


if (MessageType.SMSC_DEL_RECEIPT.containedIn(deliverSm.getEsmClass())) {
// this message is delivery receipt
try {
    DeliveryReceipt delReceipt = deliverSm.getShortMessageAsDeliveryReceipt();

    // lets cover the id to hex string format
    long id = Long.parseLong(delReceipt.getId()) & 0xffffffff;
    String messageId = Long.toString(id, 16).toUpperCase();

    /*
    * you can update the status of your submitted message on the
    * database based on messageId
    */

    System.out.println("Receiving delivery receipt for message '" + messageId + 
        " ' from " + deliverSm.getSourceAddr() + " to " + 
        deliverSm.getDestAddress() + " : " + delReceipt);

    Memo.myTextArea.append("Receiving delivery receipt for message '" + messageId + 
        " ' from " + deliverSm.getSourceAddr() + " to " + 
        deliverSm.getDestAddress() + " : " + delReceipt + "\n");      

    } 
      catch (InvalidDeliveryReceiptException e) 
      {
       System.err.println("Failed getting delivery receipt");
      }
} 
else 
{
    // this message is regular short message

    /*
    * you can save the incoming message to database.
    */ 
    try {
     int enc = deliverSm.getDataCoding();  
     String unicodeMsg = deliverSm.getShortMessage() == null ? "" : new String(new String(deliverSm.getShortMessage(),"UTF-16BE"));
  
     String engMsg = new String(deliverSm.getShortMessage());
  
     String shortMsg = null;
     if (enc == 8)
        shortMsg = unicodeMsg;
     else
        shortMsg = engMsg;
        

     System.out.println("Receiving message from " + deliverSm.getSourceAddr() +
        ": Seq No = " + deliverSm.getSequenceNumber() + ", " + shortMsg);
              Memo.myTextArea.append("Receiving: Source Addr = " + deliverSm.getSourceAddr() +
                    ": Seq No = " + deliverSm.getSequenceNumber() + 
                      ", Short Msg = " + shortMsg + ", EncType=" + enc + "\n");
                
            } catch (UnsupportedEncodingException ex) {
                System.out.println("Error : " + ex.toString());
                Memo.myTextArea.append("Error: " + ex.toString());
            }
}
}

 
    @Override
public void onAcceptAlertNotification(AlertNotification alertNotification) {
}

  
    @Override
public DataSmResult onAcceptDataSm(DataSm dataSm, Session source)
throws ProcessRequestException {

return null;
}
}

